from tortoise.fields import CharField, ForeignKeyField, IntField, BigIntField, JSONField
from tortoise.models import Model


class Contest(Model):
    contest_id = IntField(pk=True)


class Team(Model):
    team_id = IntField(pk=True)


class Rounds(Model):
    id = BigIntField(pk=True)  # 使用自动递增的id作为主键
    round_id = IntField()  # 业务轮次ID，可以重复
    contest = ForeignKeyField("models.Contest", related_name="which_contest")

    class Meta:
        unique_together = [("round_id", "contest")]  # 业务逻辑唯一约束


class RoundTeam(Model):
    id = BigIntField(pk=True)
    round = ForeignKeyField("models.Rounds", related_name="which_round")
    team = ForeignKeyField("models.Team", related_name="which_team")
    position = IntField()  # 1, 2, 3, 4 之类的

    class Meta:
        unique_together = [
            ("round", "team"),  # 约束：一个空位只能给一个队伍（固定模版）
        ]


class Problem(Model):
    problem_id = IntField(pk=True)
    content = CharField(max_length=255)


class Score(Model):
    id = IntField(pk=True)
    team = ForeignKeyField("models.RoundTeam", related_name="scores_for_which_team")
    score = JSONField(default=list)  # 每轮批次存取一组裁判的打分，总共三个批次
